package com.cssy.yd.basic.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.basic.dao.BasicBomDAO;
import com.cssy.yd.basic.dao.BasicBomRalDAO;
import com.cssy.yd.basic.entity.BasicBomDO;
import com.cssy.yd.basic.entity.BasicBomRalDO;
import com.cssy.yd.basic.service.IbasicBomRalService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;

@Service("basicBomRalService")
public class BasicBomRalServiceImpl implements IbasicBomRalService {

	@Resource(name="basicBomRalDAO")
	private BasicBomRalDAO basicBomRalDAO;
	
	@Resource(name="basicBomDAO")
	private BasicBomDAO basicBomDAO;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<BasicBomRalDO>  list=basicBomRalDAO.find(scs);
		rs.setRecordCount(basicBomRalDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {

		basicBomRalDAO.deleteBasicBomRalDOByPrimaryKey(id);
		return null;
	}

	@Override
	public Integer update(BasicBomRalDO t, User user) throws SyBaseException {
		return basicBomRalDAO.updateBasicBomRalDO(t);
	}

	@Override
	public BasicBomRalDO save(BasicBomRalDO t, User user)
			throws SyBaseException {
		t.setId(IDCodeTypeUtil.getId());
		basicBomRalDAO.insertBasicBomRalDO(t);
		return t;
	}

	@Override
	public BasicBomRalDO findListById(String id) throws SyBaseException {
		return basicBomRalDAO.findBasicBomRalDOByPrimaryKey(id);
	}

	@Override
	public List<BasicBomRalDO> findcode(String s) throws SyBaseException {
		return basicBomRalDAO.findcode(s);
	}

	@Override
	public List<BasicBomRalDO> findListByExample(BasicBomRalDO basicBomRalDO)
			throws SyBaseException {
		List<BasicBomRalDO> list=  basicBomRalDAO.findListByExample(basicBomRalDO);
		return list;
	}

	@Override
	public void bomRalSave(HttpServletRequest request,
			BasicBomRalDO basicBomRalDO) throws SyBaseException {
		try {
			String itemdata =request.getParameter("itemdata");
			String itemdelete =request.getParameter("itemdelete");
			Integer type =Integer.parseInt(request.getParameter("type"));
			String pid =request.getParameter("id");
			String turnoffName =request.getParameter("turnoffName");
			String parentid =request.getParameter("parentid");
			String parentTurnoffName =request.getParameter("parentTurnoffName");
			String turnoffCode =request.getParameter("turnoffCode");
			String turnoffNameSpec =request.getParameter("turnoffNameSpec");
			String attribute =request.getParameter("attribute");
			String turnoffNameUnit =request.getParameter("turnoffNameUnit");
			Integer useStatus =Integer.parseInt(request.getParameter("useStatus"));
			BasicBomDO basicBomDO = new BasicBomDO();
			basicBomDO.setBillstatus(type);
			basicBomDO.setId(pid);
			basicBomDO.setTurnoffName(turnoffName);
			basicBomDO.setParentid(parentid);
			basicBomDO.setParentTurnoffName(parentTurnoffName);
			basicBomDO.setTurnoffCode(turnoffCode);
			basicBomDO.setTurnoffNameSpec(turnoffNameSpec);
			basicBomDO.setAttribute(attribute);
			basicBomDO.setTurnoffNameUnit(turnoffNameUnit);
			basicBomDO.setUseStatus(useStatus);
			List<BasicBomRalDO> bbrdate = JSONArray.parseArray(itemdata,
					BasicBomRalDO.class);
					if (itemdelete != null && !"".equals(itemdelete)) {
						List<BasicBomRalDO> bbrdel = JSONArray.parseArray(itemdelete, BasicBomRalDO.class);
						for (int i = 0; i < bbrdel.size(); i++) {
							if (bbrdel.get(i).getId() != null&& !"".equals(bbrdel.get(i).getId())) {
								basicBomRalDAO.deleteBasicBomRalDOByPrimaryKey(bbrdel.get(i).getId());
							}
						}
					}
					for (int i = 0; i < bbrdate.size(); i++) {
						bbrdate.get(i).setSunid(pid);
						bbrdate.get(i).setSunname(turnoffName);
						if (bbrdate.get(i).getId() != null&& !"".equals(bbrdate.get(i).getId())) {
							basicBomRalDAO.updateBasicBomRalDO(bbrdate.get(i));
							basicBomDAO.updateBasicBomDO(basicBomDO);
						} else {
							bbrdate.get(i).setId(IDCodeTypeUtil.getId());
							basicBomRalDAO.insertBasicBomRalDO(bbrdate.get(i));
						}
					}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}


}
